package com.education.platform.api.service.impl;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.service.AccountService;
import com.education.platform.api.service.CoursewareService;
import com.education.platform.api.service.common.CacheDataService;
import com.education.platform.common.enums.RelTypeEnum;
import com.education.platform.common.exception.AssertException;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.qiniu.QiNiuService;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.CourseDao;
import com.education.platform.dao.CoursewareDao;
import com.education.platform.dao.RelUserPaperDao;
import com.education.platform.dao.WatchVideoTimeDao;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.dao.entity.CoursewareDO;
import com.education.platform.dao.entity.RelUserPaperDO;
import com.education.platform.dao.model.CoursewareModel;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* describe: 课件
* @author : tomato
* @date: 2020-06-16 11:50:03.122
**/
@Service
public class CoursewareServiceImpl extends BaseServiceImpl<CoursewareDao, CoursewareDO> implements CoursewareService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CoursewareServiceImpl.class);

    @Autowired
	private CoursewareDao coursewareDao;
    @Autowired
    private QiNiuService qiNiuService;
    @Autowired
    private AccountService AccountService;
    @Autowired
    private CacheDataService cacheDataService;
    @Autowired
    private WatchVideoTimeDao watchVideoTimeDao;
    @Autowired
    private CourseDao courseDao;
@Autowired
private RelUserPaperDao relUserPaperDao;
    /**
     * 获取视频七牛链接
     *
     * @param coursewareId
     * @return
     */
    @Override
    public String getQiNiuUrl(Integer coursewareId) {
        CoursewareDO coursewareDO = coursewareDao.getById(coursewareId);
        if(coursewareDO == null|| StringUtils.isEmpty(coursewareDO.getUrl())){
            AssertException.throwBusinessEp("课件不存在");
        }
        Integer courseId = coursewareDO.getCourseId();
        CourseDO course =  courseDao.getById(courseId);

        if(course == null){
            throw new BusinessException("课程不存在");
        }
        this.checkPermission(course);
        return qiNiuService.getPrivateDownLoadUrl(coursewareDO.getUrl());
    }

    /**
     * 检查
     * @param courseDO
     */
    private void checkPermission( CourseDO courseDO){
        //免费课程  不用验证
        if(courseDO.getPrice()==0){
            return;
        }
        //付费课程，必须登录，验证是否购买
        if (StringUtils.isEmpty(ThreadUser.getUserTokenRedisKey())) {
            throw new BusinessException("付费课程，请先登录！");
        }else{
            RelUserPaperDO r = new RelUserPaperDO();
            r.setRelType(RelTypeEnum.COURSE.getValue());
            r.setPaperId(courseDO.getId());
            r.setUserId(ThreadUser.getUserId());
            r.setDeleted(false);
            List list = relUserPaperDao.getList(r);
            if (CollectionUtils.isEmpty(list)){
                throw new BusinessException("请先购买课程");
            }
        }
    }


    @Override
    public List<CoursewareModel> getListByCourseIdAndUserId(Integer courseId, Integer userId) {
        return coursewareDao.getListByCourseIdAndUserId(courseId,userId);
    }
}
